import 'package:flutter/material.dart';
import 'package:side_navigation/side_navigation.dart';
import 'package:provider/provider.dart';
import '../provider/chatmanagerprovider.dart';
import 'views_chat.dart';

class NavigatePage extends StatefulWidget {
  const NavigatePage({Key? key}) : super(key: key);

  @override
  _NavigatePageState createState() => _NavigatePageState();
}

class _NavigatePageState extends State<NavigatePage> {
  /// Views to display

  List<Widget> views = SingletonView.viewsOfNavigate;

  int selectedIndex = 0;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<ChatManagerProvider>(
      builder: (context, navigateState, child) {
        navigateState.addListener(() {
          if (selectedIndex != navigateState.navigateIndex) {
            print("------------------");
            setState(() {
              selectedIndex = navigateState.navigateIndex;
            });
          }
        });

        return Scaffold(
          body: Row(
            children: [
              /// Pretty similar to the BottomNavigationBar!
              SideNavigationBar(
                expandable: false,
                initiallyExpanded: false,
                selectedIndex: selectedIndex,
                items: const [
                  SideNavigationBarItem(
                    icon: Icons.chat,
                    label: '消息',
                  ),
                  SideNavigationBarItem(
                    icon: Icons.person,
                    label: '通讯录',
                  ),
                  SideNavigationBarItem(
                    icon: Icons.settings,
                    label: '设置',
                  ),
                ],
                onTap: (index) {
                  navigateState.changeNavigate(index);
                },
              ),

              /// Make it take the rest of the available width
              Expanded(
                child: views.elementAt(selectedIndex),
              )
            ],
          ),
        );
      },
    );
  }
}
